<template>
  <div class="content">
    <dt-search ref="search" :label-width="90" @reset="resetFun">
      <Button type="primary" v-if="unlocking('0214010100') && name == 'yearend'" @click="onCreate">
        新建年底奖金
      </Button>
    </dt-search>

    <Tabs v-if="name == 'yearendApproval'" type="card" :animated="false" @on-click="onTab" v-model="tabName">
      <TabPane label="待审批" name="0"></TabPane>
      <TabPane label="已通过" name="1"></TabPane>
      <TabPane label="已驳回" name="2"></TabPane>
    </Tabs>

    <dt-grid ref="table" page :onFetch="onFetch" />

    <new-modal ref="new" />
    <process-modal ref="process" />
  </div>
</template>

<script>
  import moment from 'moment'
  import Column from './opt/Column.js'
  import ApprovalColumn from './opt/ApprovalColumn.js'
  import SearchOpt from './opt/SearchOpt.js'

  import newModal from './components/new-modal.vue'
  import processModal from '@/components/biz/audit/process-modal.vue'

  import billMgr from '@/biz/bonus/bill'
  import yearMgr from '@/biz/bonus/yearend'
  import flowMgr from '@/biz/flowApproval/processAudit'
  import auditMgr from '@/biz/flowApproval/processAudit'
  import dt from '@dt/dt'
  export default {
    components: {
      newModal,
      processModal
    },
    data() {
      let tabName = '0'
      let name = this.$route.name
      let Col = name == 'yearend' ? Column : ApprovalColumn
      return {
        tabName,
        name,
        column: new Col(tabName)
      }
    },
    mounted() {
      let retainParams = dt.memory.get('yearend')

      this.$refs.table.init(this, this.column, {
        status: retainParams?.state
      })
      let search = {}
      if (this.name == 'yearend' && !retainParams?.state) search.bonusYear = moment().format('YYYY')
      this.$refs.search.init(this.$refs.table, new SearchOpt(this.name), search)
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        if (this.name == 'yearend') {
          dt.memory.get('yearend')?.UContainSelf !== undefined &&
            (data.containSelf = !dt.memory.get('yearend'))
          return yearMgr.list(data)
        } else {
          data.auditType = 12
          data.buttonType = Number(this.tabName)
          data.sortMethod = this.tabName == '0' ? 'asc' : 'desc'
          return flowMgr.list(data)
        }
      },
      resetFun() {
        dt.memory.remove('yearend')
      },
      onTab(name) {
        this.$refs.table.init(this, new ApprovalColumn(this.name, name))
        this.$refs.table.fetch(true)
      },
      onCreate() {
        this.$refs.new.show()
      },
      onDetail(item) {
        this.$router.push({
          path: this.name == 'yearend' ? 'yearend/detail' : 'yearendApproval/detail',
          query: {
            id: this.name == 'yearend' ? item.bonusId : item.businessId
          }
        })
      },
      onAudit(item) {
        this.$router.push({
          path: 'yearendApproval/detail',
          query: {
            id: item.businessId,
            auditId: item.auditId
          }
        })
      },
      onEdit(item) {
        this.$router.push({
          name: 'yearendEdit',
          query: {
            id: item.bonusId
          }
        })
      },
      onResend(item) {
        billMgr.reSubmit(item.bonusId).then((res) => {
          this.$refs.table.fetch()
          this.$router.push({
            name: 'yearendEdit',
            query: {
              id: res
            }
          })
        })
      },
      onRecall(item) {
        this.$Modal.confirm({
          title: '是否确认撤回该奖金单？',
          onOk: () => {
            flowMgr
              .back({
                auditType: 12,
                businessId: item.bonusId
              })
              .then((res) => {
                this.$refs.table.fetch()
              })
          }
        })
      },
      onFlow(item) {
        this.$refs.process.show({
          auditType: 12,
          businessId: this.name == 'yearend' ? item.bonusId : item.businessId
        })
      },
      onDelete(item) {
        this.$Modal.confirm({
          title: '是否确认删除该奖金单？',
          onOk: () => {
            billMgr.del(item.bonusId).then((res) => {
              this.$refs.table.fetch()
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }
</style>
